import 'dart:async';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:fl_dit_verify/fl_dit_verify.dart';
import 'package:fl_dit_verify/umeng_verify_sdk.dart';
import 'package:fl_dit_verify_example/router.dart';
import 'package:fl_dit_verify_example/router_observer.dart';
import 'package:fl_dit_verify_example/style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';

void main() {
  runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    FlutterError.onError = (detail) {
      showLog('flutter error1:: ${detail.exceptionAsString()}');
      showLog('flutter error2:: ${detail.stack}');
    };
    runApp(const MyApp());
  }, (error, stack) {
    showLog('flutter zone onError:: $error \n stack info = $stack');
  }, zoneSpecification: ZoneSpecification(handleUncaughtError: (Zone self,
      ZoneDelegate parent, Zone zone, Object error, StackTrace stackTrace) {
    showLog('flutter zone UncaughtError:: $error \n stack info = $stackTrace');
  }));
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    if (GetPlatform.isAndroid) {
      SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
      ));
    }
    return LayoutBuilder(builder: (context, constraints) {
      if (constraints.maxWidth != 0) {
        return initApp();
      } else {
        return Container();
      }
    });
  }

  Widget initApp() {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      child: GetMaterialApp(
        title: '谛听招标',
        theme: theme,
        debugShowCheckedModeBanner: false,
        initialRoute: initialRoute(),
        // unknownRoute:
        // GetPage(name: '/notFound', page: () => const UnknownRoutPage()),
        getPages: AppRouter.pages,
        localizationsDelegates: GlobalMaterialLocalizations.delegates,
        supportedLocales: const [Locale("zh", "CN")],
        routingCallback: AppRouter.observer,
        navigatorObservers: [RouterObserver.getInstance()],
      ),
    );
  }

  String initialRoute() {
    return AppRouter.INITIAL;
    // return '/firstPage';
  }
}

///友盟一键登录
class VerifyConfig {
  static String androidVerifyAppKey = '65431b2b58a9eb5b0afdbff9';
  static String androidAppSecret =
      '1SjJLNv6osR1UPDAo6e6VeAEXRr4dLRPm17UaCT50Ayc99+RYGGdMr9ZY0e7za1MgrlpyXK1CUEX3rvE5rux1DD6PDZe/4MjgXD/rxvftFFFXGfTMnPrDUxovp0/slERW+vxq6XWlG2ge7uZiKpx49icmOh8QsJbPNDdgO/SfGkSD8E8bC6IXbT3/bfEn4IvFITkJabkIgPZD98CWsNqYf1uL5bfn/MycrrHqleHnI3xWNoBNtzCWY+in5mFlGkOxle9hnyFgj7zmEu8Abu9OX4NP3cFXtFiWVl1XmDvNMlpdY6tLVOFfQ==';
  static String iosVerifyAppKey = '65431ba2b2f6fa00ba73bfab';
  static String iosAppSecret =
      'Pq3tZSaW25ts7NUO5trtaUrIhvGsZOGledIblB2/J9fTv5NVYEZIgDni//uFOUr9HlNNi+2Vr0JTQDzgiIcJs5QbyVvuuJ1KHSF1V52dDQLJEiqllT/66mwZlynFykLhgVOXQ5LVGDMQGLvp6nrnirzOCHwzyPeAfTM3OFC3WLpjIJvPZGH6ocUV9/toH60g9vBFVPpBpyuWb1+AQqcaIqiIwUySnWrBgChC+YYpsgxTK7SnKESmLiF5Jt2fMmC975UdzP1KMGk=';
}

class Device {
  static bool get isAndroid => Platform.isAndroid;

  static bool get isIOS => Platform.isIOS;

  static late AndroidDeviceInfo _androidInfo;
  static late IosDeviceInfo _iosDeviceInfo;
  static PackageInfo? _packageInfo;

  static Future<void> initDeviceInfo() async {
    final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (isAndroid) {
      _androidInfo = await deviceInfo.androidInfo;
    } else if (isIOS) {
      _iosDeviceInfo = await deviceInfo.iosInfo;
    }
  }

  /// 使用前记得初始化
  static int getAndroidSdkInt() {
    if (isAndroid) {
      return _androidInfo.version.sdkInt;
    } else {
      return -1;
    }
  }

  static String get versionName => _packageInfo?.version ?? '1.0.0';

  static String get versionCode => _packageInfo?.buildNumber ?? '1';

  static Future<void> initPlatformInfo() async {
    _packageInfo = await PackageInfo.fromPlatform();
  }
}

String logInfo = '';

void showLog(String msg, {bool plus = false}) {
  if (plus) {
    logInfo = '$logInfo -- \n\n\n\n$msg\n';
    debugPrint('\nlogInfo::$logInfo');
  }
  debugPrint(msg);
}
